package cn.wolfcode.trip.app.controller;

import cn.wolfcode.trip.app.util.UserContext;
import cn.wolfcode.trip.base.domain.Comments;
import cn.wolfcode.trip.base.domain.Letter;
import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.query.CommentQuery;
import cn.wolfcode.trip.base.service.ICommentService;
import cn.wolfcode.trip.base.service.ILetterService;
import cn.wolfcode.trip.base.util.DateUtil;
import cn.wolfcode.trip.base.util.JSONResult;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("letter")
public class LetterController {

    @Autowired
    private ILetterService letterService;

    // 查询未读私信数量
    @GetMapping("/getLetterByState")
    public Object getMessagesByState(Long userId,HttpServletRequest request, HttpServletResponse response) {
        int data = letterService.getLetterByState(userId);
        return data;
    }

    // 查询所有私信
    @GetMapping("/getLetters")
    public Object getLetters(Long userId) {
        return letterService.getLetters(userId);
    }

    // 添加私信记录
    @PostMapping("/insertLetter")
    public JSONResult insertLetter(Letter letter) {
        JSONResult result = new JSONResult();
        try {
            User user = UserContext.getCurrentUser();
            letter.setSenderHeadUrl(user.getHeadImgUrl());
            letter.setSenderId(user.getId());
            letter.setSendername(user.getNickName());
            letterService.insertLetter(letter);
        }catch (Exception e){
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;
    }

    // 修改私信状态
    @PostMapping("/setLetterState")
    public JSONResult setLetterState(int state) {
        JSONResult result = new JSONResult();
        try {
            letterService.setLetterState(state);
        }catch (Exception e){
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;
    }

    // 查询两人通信的所有信息
    @GetMapping("/getDoubleLetters")
    public Object getDoubleLetters(Long userId,Long targetId) {
        List<Letter> list = letterService.getDoubleLetters(userId,targetId);
        /*JSONArray jsonArray = new JSONArray();
        for(Letter letter:list){
            JSONObject jsonLog = new JSONObject();
            jsonLog.put("id",letter.getId());
            jsonLog.put("senderId",letter.getSenderId());
            jsonLog.put("receiverId",letter.getReceiverId());
            jsonLog.put("sendername",letter.getSendername());
            jsonLog.put("senderHeadUrl",letter.getSenderHeadUrl());
            jsonLog.put("content",letter.getContent());
            String create_time = DateUtil.date2string(letter.getSendertime(),"yyyy-MM-dd HH-mm-ss");
            jsonLog.put("sendertime",create_time);

            jsonLog.put("state", letter.getState());
            jsonLog.put("type",letter.getType());
            jsonArray.add(jsonLog);
        }*/

        return list;
    }
}
